package com.bsh.repository;

import javax.annotation.Resource;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import com.bsh.mybatis.IRepository.ArticleMapper;
import com.bsh.mybatis.model.ArticleWithBLOBs;


@Repository
public class DaoArticle {
	@Resource
	SqlSessionTemplate session;
	@Resource
	JdbcTemplate jdbcTemplate;
	@Resource
	NamedParameterJdbcTemplate namedJdbcTemplate;
	
	ArticleMapper getMapper(){
		return session.getMapper(ArticleMapper.class);
	}

	public int deleteByPrimaryKey(Integer id) {
		int res = getMapper().deleteByPrimaryKey(id);
		return res;
	}

	public int insert(ArticleWithBLOBs record) {
		int res = getMapper().insert(record);
		return record.getId();
	}

	public int insertSelective(ArticleWithBLOBs record) {
		int res = getMapper().insertSelective(record);
		return record.getId();
	}

	public ArticleWithBLOBs selectByPrimaryKey(Integer id) {
		ArticleWithBLOBs row = getMapper().selectByPrimaryKey(id);
		return row;
	}

	public int updateByPrimaryKeySelective(ArticleWithBLOBs record) {
		int res = getMapper().updateByPrimaryKeySelective(record);
		return res;
	}

	public int updateByPrimaryKey(ArticleWithBLOBs record) {
		int res = getMapper().updateByPrimaryKeyWithBLOBs(record);
		return res;
	}

	/**
	 * 按tag查询
	 * @param tag
	 * @return
	 */
	public ArticleWithBLOBs findByTag(String tag){
		return getMapper().findByTag(tag);
	}
	
/*	*//**
	 * 添加用户
	 * @param userid
	 * @return
	 *//*
	public Integer addArticle(Article user){
		ArticleMapper map = session.getMapper(ArticleMapper.class);
		return map.addArticle(user);
	}
	
	*//**
	 * 修改用户
	 * @param userid
	 * @return
	 *//*
	public Integer editArticle(Article user){		
		ArticleMapper map = session.getMapper(ArticleMapper.class);
		return map.editArticle(user);
	}
	
	*//**
	 * 禁用用户
	 * @param userid
	 * @return
	 *//*
	public Integer enableArticle(int id,int isEnabele){		
		ArticleMapper map = session.getMapper(ArticleMapper.class);
		Integer res = map.enableArticle(id, isEnabele);
		return res;
	}	
	
	*//**
	 * 分页查询
	 *//*
	public List<Map<String,Object>> getPage(Pager pager) {				
		StringBuilder sb=new StringBuilder();
		MapSqlParameterSource ps = new MapSqlParameterSource();
		sb.append("select * from user");
		Integer count= namedJdbcTemplate.queryForInt("select count(0) from ("+sb.toString()+")v",ps);
		
		sb.append(" order by id desc limit :start,:size");		
		ps.addValue("start", pager.getStart());
		ps.addValue("size", pager.getPageSize());
		List<Map<String,Object>> list=namedJdbcTemplate.queryForList(sb.toString(), ps);

		pager.setRowCount(count);
		return list;
	}


	*//**
	 * 取得登录用户信息
	 *//*
	public Article getLoginArticle(String userName, String password) {
		ArticleMapper dao = session.getMapper(ArticleMapper.class);
		Article user = dao.getLoginArticle(userName, password);
		return user;
	}
	
	*//**
	 * 修改密码
	 *//*
	public int updatePassword(int id, String password,String newPassword){
		ArticleMapper dao = session.getMapper(ArticleMapper.class);
		int res = dao.updatePassword(id, password,newPassword);
		return res;
	}
	
	*//**
	 * 重置密码
	 *//*
	public int resetPassword(String mobile, String password, String smsVerifyCode){
		ArticleMapper dao = session.getMapper(ArticleMapper.class);
		int res = dao.resetPassword(mobile, password,smsVerifyCode);
		return res;
	}*/
}